package longrise.phone.com.bjjt_henan.myaccident;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.longrise.android.FormParameter;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.LFView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.RecycleBitmap;
import uk.co.senab.photoview.PhotoView;


public class WebViewLoad extends LFView implements View.OnClickListener, ILFMsgListener {
    private Context context = null;
    private View view = null;
    private LinearLayout hn_btn_back;
    private PhotoView iv_one;
    private ProgressBar myProgressBar;
    private String title;
    private String url;

    public WebViewLoad(Context c, String title, String url) {
        super(c);
        this.context = c;
        this.title = title;
        this.url = url;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.webviewload, null);
        if (null == view) {
            return;
        }
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        iv_one = (PhotoView) view.findViewById(R.id.iv_one);
        myProgressBar = (ProgressBar) view.findViewById(R.id.myProgressBar);

        tv_title.setText(title);
        initview();
    }


    private void initview() {
        ImageLoader.getInstance().displayImage(url, iv_one, getOptions(), null,
                new ImageLoadingProgressListener() {
                    @Override
                    public void onProgressUpdate(String s, View view, int i, int max) {
                        myProgressBar.setMax(max);
                        if (i == max) {
                            myProgressBar.setVisibility(View.INVISIBLE);
                        } else {
                            if (View.GONE == myProgressBar.getVisibility()) {
                                myProgressBar.setVisibility(View.VISIBLE);
                            }
                            myProgressBar.setProgress(i);
                        }
                    }
                });

    }

    private DisplayImageOptions options;

    //实例化imageloader的参数
    public DisplayImageOptions getOptions() {
        if (null == options) {
            DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
            builder.cacheInMemory(true);
            builder.cacheOnDisk(true);
            builder.imageScaleType(ImageScaleType.EXACTLY_STRETCHED);
            options = builder.build();
        }
        return options;
    }

    private void regEvent(boolean b) {
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
        if (b) {
            this.addILFMsgListener(this);
        } else {
            this.removeILFMsgListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hn_btn_back:
                OnDestroy();
                closeForm();
                break;
            default:
                break;
        }
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10) {//点击返回按键
            OnDestroy();
            closeForm();
        }
        return null;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        regEvent(false);
        view = null;
        RecycleBitmap.recycleImageView(iv_one);
        ImageLoader.getInstance().clearDiskCache();
        ImageLoader.getInstance().clearMemoryCache();
    }
}
